<template>
  <el-row :gutter="20">
    <el-col :span="4">
      <el-card>
        <div class="title">
          <div class="round">
            <el-icon>
              <Document />
            </el-icon>
          </div>
          <h4>今日总收入（元）</h4>

        </div>
        <div class="total mt">
          <h1>{{ formatNumberToThousands(12239824) }}</h1>
          <div class="percent">-21%</div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="4">
      <el-card>
        <div class="title">
          <div class="round">
            <el-icon>
              <Document />
            </el-icon>
          </div>
          <h4>本月总收入 (万元)</h4>
        </div>
        <div class="total mt">
          <h1>{{ formatNumberToThousands(2924) }}</h1>
          <div class="percent">-21%</div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="4">
      <el-card>
        <div class="title">
          <div class="round">

            <el-icon>
              <Document />
            </el-icon>
          </div>
          <h4>会员卡储值金额 (元)</h4>
        </div>
        <div class="total mt">
          <h1>239,824</h1>
          <div class="percent">-16%</div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="4">
      <el-card>
        <div class="title">
          <div class="round">
            <el-icon>
              <Document />
            </el-icon>
          </div>
          <h4>服务费总金额 (元)</h4>
        </div>
        <div class="total mt">
          <h1>{{ formatNumberToThousands(16824) }}</h1>
          <div class="percent">-7%</div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="4">
      <el-card>
        <div class="title">
          <div class="round">
            <el-icon>
              <Document />
            </el-icon>
          </div>
          <h4>停车费总金额 (元)</h4>
        </div>
        <div class="total mt">
          <h1>{{ formatNumberToThousands(9687) }}</h1>
          <div class="percent">-4%</div>
        </div>
      </el-card>
    </el-col>
    <el-col :span="4">
      <el-card>
        <div class="title">
          <div class="round">
            <el-icon>
              <Document />
            </el-icon>
          </div>
          <h4>电费总金额 (元)</h4>
        </div>
        <div class="total mt">
          <h1>{{ formatNumberToThousands(223674) }}</h1>
          <div class="percent">-19%</div>
        </div>
      </el-card>
    </el-col>
  </el-row>
  <!-- 数据图 -->
  <el-card class="mt">
    <div ref="chartRef" style="width: 100%;height: 300px;"> </div>
  </el-card>

  <!-- 表格功能部分 -->
  <el-card class="mt">
    <el-input v-model="name" style="max-width: 400px;" placeholder="请输入站点名称">
      <template #append>
        <el-button icon="Search" @click="loatTableData"></el-button>
      </template>
    </el-input>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="index" label="序号" width="80"></el-table-column>
      <el-table-column label="充电站名称" prop="name"></el-table-column>
      <el-table-column label="充电站ID" prop="id"></el-table-column>
      <el-table-column label="所属城市" prop="city"></el-table-column>
      <el-table-column label="充电桩总量(个)" prop="count"></el-table-column>
      <el-table-column label="单日总收入(元)" prop="day" sortable>
        <template #default="scope">
          <span>{{ scope.row.day }}</span>
          <el-tag :type="scope.row.percent > 0 ? 'danger' : 'success'" class="ml">
            {{ scope.row.percent > 0 ? "+" + scope.row.percent + "%" : scope.row.percent + "%" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="月度总收入(万元)" prop="month">
        <template #default="scope">
          <span>{{ scope.row.month }}</span>
          <el-tag :type="scope.row.mpercent > 0 ? 'danger' : 'success'" class="ml">
            {{ scope.row.mpercent > 0 ? "+" + scope.row.mpercent + "%" : scope.row.mpercent + "%" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="电费营收(元)" prop="electricity"></el-table-column>
      <el-table-column label="停车费营收(元)" prop="parkingFee"></el-table-column>
      <el-table-column label="服务费营收(元)" prop="serviceFee"></el-table-column>
      <el-table-column label="会员储值金(元)" prop="member"></el-table-column>
    </el-table>
  </el-card>
  <el-pagination style="display: flex;justify-content: flex-end;margin-bottom: 40px;" v-model:current-page="pageInfo.page" v-model:page-size="pageInfo.pageSize"
    :page-sizes="[10, 20, 30, 40]" layout="sizes, prev, pager, next, jumper,total" :total="totals"
    @size-change="handleSizeChange" @current-change="handleCurrentChange" background />
</template>



<script setup lang="ts">
import formatNumberToThousands from "@/utils/toThousands"
import { onMounted, reactive, ref } from "vue";
import { useChart } from "@/hooks/userChart";
import { chartApi, revenueApi } from "@/api/chargingstation";
import { usePagination } from "@/hooks/usePagination";
// 数据图表
const chartRef = ref(null);

const setChartData = async () => {
  const chartOptions = reactive({
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: []
    },
    xAxis: {
      type: 'category',
      data: ['一月', '二月', '三月', '四月', '五月', '六月', '七月']
    },
    yAxis: [
      {
        type: 'value',
        name: '销售',
        position: 'left'
      },
      {
        type: 'value',
        name: '访问量',
        position: 'right'
      }
    ],
    series: [
      {
        name: '',
        type: 'bar',
        data: [],
        yAxisIndex: 0,
        itemStyle: {
          color: '#409eff'
        }
      },
      {
        name: '',
        type: 'line',
        data: [],
        yAxisIndex: 1,
        itemStyle: {
          color: '#409eff'
        },
        smooth: true
      }
    ]
  });
  const res = await chartApi()
  chartOptions.legend.data = res.data.list.map((item: any) => item.name);
  for (let i = 0; i < res.data.list.length; i++) {
    chartOptions.series[i].name = res.data.list[i].name
    chartOptions.series[i].data = res.data.list[i].data
  }
  return chartOptions
}
useChart(chartRef, setChartData)

//表格
const name = ref('')
const tableData = ref([]);
const loading = ref(false);
const loatTableData = async () => {
  loading.value = true
  const { data: { list, total } } = await revenueApi({ ...pageInfo,name:name.value})
  loading.value = false
  setTools(total)

  tableData.value = list
  tableData.value = list.map((item: any) => ({
    ...item,
    day: item.electricity + item.parkingFee + item.serviceFee + item.member
  }))
}

const { pageInfo,
  handleSizeChange,
  handleCurrentChange,
  setTools,
  totals,
} = usePagination(loatTableData)

onMounted(() => {
  loatTableData()
})
</script>

<style lang="less">
.title {
  display: flex;
  align-items: center;

  .round {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background-color: rgb(235, 236, 245);
    text-align: center;
    line-height: 30px;
    margin-right: 20px;
  }

  h4 {
    color: #666;
  }
}

.total {
  display: flex;
  align-items: center;

  h1 {
    font-size: 30px;
    margin-right: 20px;
  }

  .percent {
    display: inline-block;
    padding: 3px 5px;
    height: 20px;
    font-size: 12px;
    background-color: rgb(235, 247, 239);
    border-radius: 2px;
    color: green;
    line-height: 20px;
  }
}

.mt {
  margin: 20px 0;
}
</style>